package cn.kgc.watch.service.impl;

import cn.kgc.watch.entity.Cart;
import cn.kgc.watch.entity.CartShopping;
import cn.kgc.watch.mapper.CartMapper;
import cn.kgc.watch.mapper.CartShoppingMapper;
import cn.kgc.watch.service.CartService;
import cn.kgc.watch.service.CartShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CartServiceImpl implements CartService {
    @Resource
    private CartMapper cartMapper;
    @Autowired
    private CartShoppingMapper cartShoppingMapper;
    @Autowired
    private CartShoppingService cartShoppingService;
    @Override
    public Cart findCartByUserId(Integer userId) {
        return this.cartMapper.selectCartByUserId(userId);
    }

    @Override
    public Cart findCartSku(Integer id) {
        return this.cartMapper.selectCartSku(id);
    }

    @Override
    public void addCart(Cart cart) {
        cart.setCreateTime(new Date());
        cart.setTotalPrice(0.0);
        this.cartMapper.insertCart(cart);
        CartShopping cartShopping = cart.getCartShopping();
        cartShopping.setCart(cart);
        cartShopping.setCreateTime(new Date());
        this.cartShoppingMapper.insertCartShopping(cartShopping);
        this.cartShoppingService.refresh(cartShopping.getId());
    }
}
